package com.cow.framework.core.login;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.core.base.BaseComponent;
import com.cow.framework.core.config.constant.MessageConstants;
import com.cow.framework.core.exception.BusinessException;
import com.cow.framework.utils.math.MathUtil;
import com.cow.framework.utils.web.SessionUtil;
import com.cow.support.dto.User;
import com.cow.support.dto.UserSessionModel;
import com.cow.support.remote.AuthRemoteService;


/**
 * 用户登录
 *
 * @author yangsj 
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping(value="/comm/login")
public class LoginController extends BaseComponent{
	@Autowired
	LoginService loginService;
	@Autowired
	LoginAdditional loginAdditional;
	
	/**登录*/
	@RequestMapping(value="/onLogin")	
	public Object onLogin(@RequestBody Map parmMap) throws Exception{	
		//验证邀请码
		String checkCode = parmMap.get("checkCode").toString();
		//开发模式验证码写死
		if(active.equals("dev") || active.equals("local")){
			checkCode="1234";
		}else{
			if(checkCode == null || redisUtil.get("checkCode-"+checkCode)==null){//验证码无效或已过期
				throw new BusinessException("B1006",MessageConstants.B1002);
			}
		}				
				
		String account = parmMap.get("account").toString();
		String password = (String)parmMap.get("password");
		
		User user = loginService.login(account, password,checkCode);
		UserSessionModel model = new UserSessionModel();
		model.setUserInfo(user);
		model.setSession_key(SessionUtil.getSessionId());
		//个性化设置
		loginAdditional.setCustomData(model);		
		//保存session
		redisUtil.set("userSession-"+SessionUtil.getSessionId(), model,Long.valueOf(parameterUtil.get("sessionExpireTime")));
		redisUtil.setCommon("userSession-"+SessionUtil.getSessionId(), model,Long.valueOf(parameterUtil.get("sessionExpireTime")));
		return model;
	}
	
	/**获取验证码*/
	@RequestMapping(value="/getCheckCode")	
	public Object getCheckCode() throws Exception{		
		//生成邀请码
		Integer code = MathUtil.randomInt(1000, 9999);		
		//写缓存
		redisUtil.set("checkCode-"+code,code,Long.valueOf(parameterUtil.get("checkCodeExpireTime")));

		return code;
	}
	
	/**获取当前用户信息*/
	@RequestMapping(value="/getUser")	
	public Object getUser() throws Exception{	
		//登录验证        
        String token = SessionUtil.getRequest().getHeader("Authorization");  
		UserSessionModel model = (UserSessionModel)SessionUtil.getRequest().getSession().getAttribute("userSession");
		AuthRemoteService client = super.getAuthClient();
		User user = client.loadUserByAccountname(model.getUserInfo().getAccountname(), appName);
		if(user.getResourceMap().get("t0")== null){
			throw new BusinessException("B1014",MessageConstants.B1014);
		}
		model.setUserInfo(user);
		//保存session
		redisUtil.set("userSession-"+token, model,Long.valueOf(parameterUtil.get("sessionExpireTime")));
		//写缓存
		redisUtil.set("user-"+user.getUserid(),user,Long.valueOf(parameterUtil.get("userExpireTime")));
		SessionUtil.getRequest().getSession().setAttribute("userSession", user);
		return model;
	}
}
